<?php
echo $javascript->link('ui/ui.core.js', false);
echo $javascript->link('ui/ui.draggable.js', false);
echo $javascript->link('ui/ui.droppable.js', false);

?>

<div class="workouts index calenderview content-holder-1">
	<h2><?php echo $this->pageTitle; ?></h2>
	<div class="holder">
		<div class="main">
			<div class="btn_bar holder">
				
				<?php //echo $key;
				$class = ($key == 'm') ? 'active' : '';
				echo $html->link(__('Month', true), array('action' => 'index','m', $year, $month), array('class'=>"btn-3 $class")); 
				?> <?php 
				$class = ($key == 'w') ? 'active' : '';
				echo $html->link(__('Week', true), array('action' => 'index','w', $year, $week), array('class'=>"btn-3 $class"));
				?> <?php 
				$class = ($key == 'p') ? 'active' : '';
				echo $html->link(__('Period', true), array('action' => 'index','p', $year, $period), array('class'=>"btn-3 $class"));
				?>
				<?php 
				echo $html->link(__('Today', true), array('action' => 'index', $key), array('class'=>"btn-3"));
				?>
			</div>
			
			<div class="top-bar-2 rounded-top pagina">
				<?php echo $html->link(__('Previous', true), array('action' => 'index',$key, $year, $browse-1), array('class'=>'prev left')); ?>
				<?php echo $html->link(__('Next', true), array('action' => 'index',$key, $year, $browse+1),array('class'=>'next right')); ?>
				<?php if(isset($stats)):?>
				<span>
				<?php echo $header;?>: <?php echo $stats['hours']?> <?php __('hours and') ?>  <?php echo $stats['min']?> <?php __('of training') ?></span>
				<?php else: ?>
					<?php echo date('F, Y', mktime(0, 0, 0, $month, 1, $year))?>
				<?php endif; ?>
			</div>
			
			<ul class="calendar">
				<?php foreach($weekdays as $num => $day):?>
				<li class="<?php echo $day['class']; ?> day<?php echo $num; ?>">
					<div class="day-holder">
						<?php echo $day['short']; ?>
					</div>
				</li>
				<?php endforeach; ?>
				<?php $num = 0; ?>
				<?php foreach($days as $day):?>
					<?php 
					if (($num%7)==0):?>
					<?php $weeknum = $day['week_num']; ?>
					<li class="week">
						<div class="week-title"><?php __('Week')?> <?php echo $weeknum;?> 
							<?php if (isset($day['Period'])): ?>
								- <?php echo $day['Period']['Period']['title']; ?>
								<?php echo $html->link(__('Info', true), array('controller'=>'periods','action' => 'view',$day['Period']['Period']['id']), array('class'=>'info nyroModal')); ?> 
							<?php endif; ?>
							
							</div>
						<label for="week_copy_<?php echo $weeknum; ?>">Copy workouts to</label>
						<select class="week_copy" id="week_copy_<?php echo $weeknum; ?>">
							<?php for($j=4; $j >=-4; $j--):?>
								<?php $copy_string = $day['date'] . '/' . $j; ?>
								<?php if ($j > 0): ?>
								<option value="<?php echo $copy_string;?>"><?php echo abs($j);?> week ahead</option>
								<?php elseif ($j < 0): ?>
									<option value="<?php echo $copy_string;?>"><?php echo $j;?> week before</option>
								<?php else: ?>
									<option value="null" selected="selected">Pick a week</option>
								<?php endif; ?>
							<?php endfor;?>
						</select>
					</li>
					<?php endif; $num++;?>
					<li class="<?php echo $day['class']; ?> day<?php echo $day['day_num']?>">
						<div class="day-holder" title="<?php echo $day['date']; ?>">
							<span class="day_num"><?php echo $day['short']; ?> </span>
							<?php echo $html->link(__('Add', true), array('action' => 'add', $day['date']), array('class'=>'add nyroModal')); ?>
							<div class="event-holder">
							<?php if(isset($day['Workouts'])):?>
								
								<?php foreach($day['Workouts'] as $workout):?>
									
									<?php if(isset($workout['Template']['title'])):?>
										<div class="workout-event rounded" rel="<?php echo $workout['Workout']['id'] ?>">
									<?php $title = $text->truncate($workout['Template']['title'], 15, '');
									echo $html->link($title, array('action' => 'view', $workout['Workout']['id']), array('class'=>'nyroModal')); ?>
									</div>
								<?php endif; ?>
								<?php endforeach; ?>
							<?php endif; ?>
							</div>
						</div>
					</li>

				<?php endforeach; ?>

			</ul> 
			<script type="text/javascript">
				jQuery(document).ready(function(){
					
					// Drag and move workouts inside calandar
					jQuery('.workout-event').draggable({
						cancel: 'a.ui-icon',
						revert: 'invalid',
						containment: 'ul.calendar',
						helper: 'clone',
						cursor: 'move'
					});
					
					jQuery('.day-holder').droppable({
						accept: '.workout-event',
						activeClass: 'ui-state-highlight',
						drop: function(ev, ui) {
							droppedWorkout(ui.draggable,$(this));
						}
					});
					
					var url = "<?php echo $html->url(array('action'=>'move'))?>";
					function droppedWorkout($item, $target) {
						var query = url + '/' + $item.attr('rel') + '/' + $target.attr('title');
						
						jQuery.getJSON(query, function(data){
							$item.hide();
							$item.appendTo($target.find('.event-holder')).fadeIn();
						});
					}
					
					// Copy one week of workouts to another
					jQuery("select.week_copy").change(function() {
						var val = jQuery("select.week_copy option:selected").val();
						if(val != "null") {
							//window.location = jQuery("#sort_local option:selected").val();
							alert(val);
						}
						
					});
					
				});
			</script>
			
		</div><!--End .main -->
		<div class="sidebar">
			<?php 
			if(isset($user)) {
				echo $this->element('sidebar/user', array('user'=>$user));
			}
			if(isset($templates)) {
				echo $this->element('sidebar/templates', array('templates'=>$templates));
			}
			if(isset($stats)) {
				echo $this->element('sidebar/stats', array('stats'=>$stats));
			}
			?>
			
		</div><!--End .sidebar -->
	</div><!--End .holder -->
</div>	
